home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 September
/
Macworld (1997-09).dmg
/
Shareware World
/
Utilities
/
Text Processing
/
Alpha
/
Tcl
/
SystemCode
/
wordCompletion.tcl
< prev
Wrap
Text File
|
1996-08-27
|
6KB
|
197 lines
####################################################################
#
# FILE: "wordCompletion+.tcl"
# created: 26/11/95 {1:05:48 am}
# last update: 26/11/95 {1:06:49 am}
# Author: Vince Darley
# E-mail: <mailto:vince@das.harvard.edu>
# mail: Division of Applied Sciences, Harvard University
# Oxford Street, Cambridge MA 02138, USA
# <http://www.das.harvard.edu/users/students/Vincent_Darley/>
#
# Description: Modified procedure allowing for a different
# search string for completion.
#
# History
#
# Based on Mark Nagata's Word Completion, w/ additions from Tim van der Leeuw
#
# modified by rev reason
# -------- --- --- -----------
# 26/11/95 VMD 1.0 original
# _/_/_
#
#
# From original documentation:
# This extension saves typing, as well as making sure variable names are
# correct. While typing the following C code, there is no need to type all
# of the second occurrence of verySpecialInt. One could copy and paste, but
# another way is to type a few letters, and select the Word Completion
# Extension.
#
# int verySpecialInt = 10;
# while(verySp
#
#
# becomes
#
# int verySpecialInt = 10;
# while(verySpecialInt
#
#
#
# Word Completion will look back in the code to find the first match and then
# extend the current occurrence to match the previous occurrence. If a match
# is not found looking backwards, then it looks forwards. If not found
# forwards, the word is selected. This is a quick way to save on typing,
# good for helping prevent RSI, but perhaps more importantly, to make sure
# that variable names are spelt correctly.
#
####################################################################
set __wc__insPos -1
proc wordCompletion { {got ""} {looking ""} } {
global __wc__len __wc__prevPos __wc__insPos __wc__prevFound __wc__pat __wc__nextStart __wc__fwd
set pos [getPos]
# Cursor changed place?
if $pos==$__wc__insPos {
# it is an old search
set ret [wc__newSearch $pos]
if { $ret == 1 } {
return
} elseif { $ret == -1 } {
select [expr $pos + [string length $looking] - [string length $__wc__prevFound] - [string length $got]] $pos
return
}
}
# Start new search for WordCompletion
if { $got == "" } {
# this is a normal completion
backwardWord
set start [getPos]
set one [getText $start $pos]
set __wc__len [string length $one]
set __wc__pat [append one {[\w_]+}]
} else {
# here we complete 'got' with something beginning 'looking'
set start [expr $pos - [string length $got]]
set one $looking
set __wc__len [string length $one]
set __wc__pat [append one {[^ \t\n\r]+}]
}
incr start -1
if {![catch {search -s -f 0 -r 1 -i 0 -m 1 -- $__wc__pat $start} data]} {
set d00 [lindex $data 0]
set beg [expr $d00+$__wc__len]
set end [lindex $data 1]
set __wc__prevFound [getText $d00 $end ]
set txt [getText $beg $end]
goto $pos
insertText $txt
message "Found above."
# Set a number of globals for possible next go-around
set __wc__insPos [getPos]
set __wc__prevPos $pos
set __wc__nextStart [expr $d00-$__wc__len]
set __wc__fwd 0
return
}
if {![catch {search -s -f 1 -r 1 -i 0 -m 1 -- $__wc__pat $pos} data]} {
set __wc__prevFound [getText [lindex $data 0] [lindex $data 1] ]
set beg [expr [lindex $data 0]+$__wc__len]
set end [lindex $data 1]
set txt [getText $beg $end]
goto $pos
insertText $txt
message "Found below."
# Set a number of globals for possible next go-around
set __wc__insPos [getPos]
set __wc__prevPos $pos
set __wc__nextStart $end
set __wc__fwd 1
return
}
goto $pos
#backwardWordSelect
select [expr $start +1] $pos
}
# returns '1' if it succeeded
# or -1 if failed completely
proc wc__newSearch { pos } {
global __wc__len __wc__prevPos __wc__insPos __wc__prevFound __wc__pat __wc__nextStart __wc__fwd
set skipStr $__wc__prevFound
while 1 {
if $__wc__fwd {
set fndMsg "Found below."
} else {
set fndMsg "Found above."
}
if {![catch {search -s -f $__wc__fwd -r 1 -i 0 -m 1 -- $__wc__pat $__wc__nextStart} data]} {
set d00 [lindex $data 0]
set beg [expr $d00+$__wc__len]
set end [lindex $data 1]
set __wc__prevFound [getText $d00 $end]
if [string compare $skipStr $__wc__prevFound] {
# Have we got the same word twice?
set txt [getText $beg $end]
deleteText $__wc__prevPos $__wc__insPos
goto $__wc__prevPos
insertText $txt
message $fndMsg
# Set a number of globals for possible next go-around
set __wc__insPos [getPos]
if $__wc__fwd {
# Search Forwards
set __wc__nextStart $end
# End of found word
} else {
# Search Backwards
set __wc__nextStart [expr $d00 - $__wc__len]
# Before start of found word
if $__wc__nextStart<=0 {
set __wc__fwd 1
set __wc__nextStart $__wc__insPos
}
}
return 1
} else {
# Move start of search after finding string again
if $__wc__fwd {
# Searching Forwards
set __wc__nextStart $end
# End of found word
} else {
# Still Searching Backwards
set __wc__nextStart [expr $d00 - $__wc__len]
# Before start of found word
if $__wc__nextStart<=0 {
set __wc__fwd 1
set __wc__nextStart $__wc__insPos
}
}
}
# End if string compare
} else {
# Search string not found
if $__wc__fwd {
# We were already looking forward, so the word is not in the file
message "Not found."
set __wc__insPos -1
goto $pos
return -1
} else {
# start looking forward
set __wc__fwd 1
set __wc__nextStart $__wc__insPos
}
}
}
return 0
}